package com.zzt.trans.controller;

import com.zzt.trans.service.LogService;
import com.zzt.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-03-31
 * Time: 21:03
 */
@Slf4j
@RestController
@RequestMapping("/user2")
public class UserController2 {

    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;


    @Transactional
    @RequestMapping("/registry")
    public String registry(String name, String password){

        //用户注册
        Integer result = userService.registryUser(name, password);
        log.info("用户注册成功，影响行数：" + result);
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.error("程序发生异常");
//            throw new RuntimeException(e);
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    @Transactional
    @RequestMapping("/r2")
    public String r2(String name, String password) throws IOException {

        //用户注册
        Integer result = userService.registryUser(name, password);
        log.info("用户注册成功，影响行数：" + result);
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r3")
    public String r3(String name, String password) throws IOException {

        //用户注册
        Integer result = userService.registryUser(name, password);
        log.info("用户注册成功，影响行数：" + result);
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }

    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r4")
    public String r4(String name, String password) throws IOException {

        //用户注册
        Integer result = userService.registryUser(name, password);
        log.info("用户注册成功，影响行数：" + result);
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }

    @Transactional()
    @RequestMapping("/r5")
    public String r5(String name, String password) {

        //用户注册
        Integer result = userService.registryUser(name, password);
        log.info("用户注册成功，影响行数：" + result);

        Integer logResult = logService.insertLog(name, "用户注册");
        log.info("日志记录成功，影响行数：" + logResult);

        return "注册成功";
    }
}
